package util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * 读取xml文件的工具类
 *
 * @author xym
 * @since 2020/7/15
 */
public class XmlFileUtil {
    private static final Logger log = LoggerFactory.getLogger(XmlFileUtil.class);
    private static SAXReader saxReader;

    public static Document read(String xmlFilePath) {
        if (saxReader == null) {
            saxReader = new SAXReader();
        }
        Document document = null;
        try {
            log.info("开始加载文件 - {}", xmlFilePath);
            long start = System.currentTimeMillis();

            document = saxReader.read(new File(xmlFilePath));

            long end = System.currentTimeMillis();
            log.info("加载文件完成, 耗时{} ms", (end - start));
        } catch (DocumentException e) {
            log.error("读取文件失败 - {}", xmlFilePath);
            e.printStackTrace();
        }
        return document;
    }
}
